import re
import time
import hashlib
import requests
from util.getheaders import get_init_headers

import os
import shutil
from util.logger import get_looger

logger = get_looger("bilibili.log", __file__)

logger.info("爬虫启动......")

root = 'static/bilibili/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)

url = ('https://api.bilibili.com/x/web-interface/wbi/index/top/feed/rcmd?&ps=12&brush=10')
# 使用修复后的代码

response = requests.get(url, headers=get_init_headers())

if response.status_code == 200:
    json_data = response.json()
    for item in json_data['data']['item']:
        print(item)
else:
    print("请求失败")
    logger.error(f"请求失败，状态码: {response.status_code}")


# page = 1
# while True:
#
#     url = f'https://api.bilibili.com/pgc/season/index/result?st=4&order=3&season_version=-1&is_finish=-1&copyright=-1&season_status=-1&year=-1&style_id=-1&sort=0&page={page}&season_type=4&pagesize=20&type=1'
#     headers = get_init_headers()
#
#     res = requests.get(url, headers=headers)
#     json_data = res.json()
#     logger.info(f"第{page}页: {json_data}")
#     for item in json_data['data']['list']:
#         print(item)
#
#     if json_data['data']['has_next'] ==1:
#         page += 1
#     else:
#         break
# logger.info("爬虫结束......")





